/**
 * 面对面建群业务 type 55  移动端
 * 第二版 根据ip和数字区分 拼接
 */
const computerPosition = require("./position.js")
const onfacetoface = (Item) => {
	let obj = {
		code: 200,
		type: 55,
		status: false, //群聊创建成功true 缺少参数 定位错误
		message: "面对面建群"
	}
	OperationfaceToFaceTimeV2();
	if (!Item.fromId || Item.fromName === undefined || Item.groupNumber === undefined ||
		Item.position === undefined || Item.position.longitude === undefined || Item.position.latitude === undefined
	) {
		obj.message = "缺少参数";
		if (wsClients[Item.fromId]) {
			wsClients[Item.fromId].forEach((client) => {
				client.send(JSON.stringify(obj));
			});
		}
		return;
	}
	let {
		groupNumber
	} = Item;
	// groupNumber//改成拼接的signGroupNumber
	let groupPosition = computerPosition(Item.position).groupPosition;
	if (!groupPosition) {
		obj.message = "定位错误";
		if (wsClients[Item.fromId]) {
			wsClients[Item.fromId].forEach((client) => {
				client.send(JSON.stringify(obj));
			});
		}
		return;
	}
	let signGroupNumber = groupPosition + "-" + groupNumber;
	obj.status=true
	if (faceToFaceList[signGroupNumber] === undefined) {
		faceToFaceList[signGroupNumber] = {
			groupNumber,
			signGroupNumber,
			ItemId: (new Date()).getTime(),
			arr: new Map(),
			url: "", //群图标
			describe: `[ID:${Item.fromId}-username:${Item.fromName}]创建`,
			userlist: [],
			intoUserList:new Set(),
			onlineTime: new Date()
		}
	} else {
		//已经有群信息，重置时间
		faceToFaceList[signGroupNumber].onlineTime = new Date();
	}

	faceToFaceList[signGroupNumber].arr.set(Item.fromId, Item);
	faceToFaceList[signGroupNumber].userlist = [];
	faceToFaceList[signGroupNumber].arr.forEach((value, key) => {
		faceToFaceList[signGroupNumber].userlist.push(value)
	})

	obj.data = Object.assign({}, faceToFaceList[signGroupNumber]);
	// 群发
	faceToFaceList[signGroupNumber].arr.forEach((value, key) => {
		if (wsClients[key]) {
			wsClients[key].forEach((client) => {
				client.send(JSON.stringify(obj));
			});
		}
	})
}
module.exports = onfacetoface;